<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Folder class file.
 *
 * @package   blocks-email_list
 * @author    Toni Mas <toni.mas@uib.dot.es>
 * @copyright 2008 by <toni.mas at uib dot es>
 * @license   The source code packaged with this file is Free Software, Copyright (C) 2008 by
 *          <toni.mas at uib dot es>.
 *          It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
 *          You can get copies of the licenses here:
 *                            http://www.affero.org/oagpl.html
 *          AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
 **/

/**
 * Parent class for folders.
 *
 * @author    Toni Mas <toni.mas@uib.dot.es>
 * @copyright 2008 by <toni.mas at uib dot es>
 * @license   http://www.affero.org/oagpl.html AFFERO GENERAL PUBLIC LICENSE
 **/
class folder {

    /**
     * Constructor
     */
    function folder() {
    }

    /**
     * This functions created news folders
     *
     * @param object $folder       Fields of new folder
     * @param int    $parentfolder Parent folder
     * @return bool Success/Fail
     * @uses $DB
     * @todo Finish documenting this function
     **/
    function newfolder($folder, $parentfolder) {
        global $DB;

        // Add actual time
        $folder->timecreated = time();

        // Make sure course field is not null            Thanks Ann.
        if ( ! isset( $folder->course) ) {
            $folder->course = 0;
        }

        // Insert record
        if (! $folder->id = $DB->insert_record('block_email_list_folder', $folder)) {
            return false;
        }

        // Prepare subfolder
        $subfolder = new stdClass();
        $subfolder->folderparentid = $parentfolder;
        $subfolder->folderchildid  = $folder->id;

        // Insert record reference
        if (! $DB->insert_record('block_email_list_subfolder', $subfolder)) {
            return false;
        }

        add_to_log($folder->userid, 'email_list', 'add subfolder', $folder->name);

        return true;
    }
}
